/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2021年10月24日
 */
package com.massyframework.beanskin.scriptengine.beancontext;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import javax.script.ScriptEngine;
import javax.script.ScriptException;

import com.massyframework.beanskin.Module;
import com.massyframework.beanskin.service.ServiceRepository;
import com.massyframework.beanskin.service.script.ScriptEngineBuilder;
import com.massyframework.beanskin.service.script.ScriptEngineNotFoundException;
import com.massyframework.beanskin.service.script.ScriptFile;

/**
 * 抽象的脚本引擎构建器
 * @author huangkh
 *
 */
public abstract class AbstractScriptEngineBuilder implements ScriptEngineBuilder {

	protected static final String MAIN_JS = "BEANSKIN-INF/js/main.js";
	
	private Module<?> module;
	private ServiceRepository serviceRepository;
	private Map<String, Object> objectMap;
	private List<ScriptFile> scriptFiles;
	
	/**
	 * 构造方法
	 * @param module {@link Module},模块
	 */
	public AbstractScriptEngineBuilder(Module<?> module) {
		this.module = Objects.requireNonNull(module, "\"module\" cannot be null.");
		this.serviceRepository = ServiceRepository.retrieveFrom(module);
	}

	@Override
	public AbstractScriptEngineBuilder addObject(String name, Object value) {
		if (name != null && value != null) {
			if (this.objectMap == null) {
				this.objectMap = new HashMap<>();
			}
			
			this.objectMap.put(name, value);
		}
		return this;
	}

	@Override
	public AbstractScriptEngineBuilder addObjects(Map<String, Object> value) {
		if (value != null) {
			if (this.objectMap == null) {
				this.objectMap = new HashMap<>();
			}
			
			this.objectMap.putAll(value);
		}
		return this;
	}

	@Override
	public AbstractScriptEngineBuilder addScriptFile(ScriptFile value) {
		if (value != null) {
			if (this.scriptFiles == null) {
				this.scriptFiles = new ArrayList<>();
			}
			
			this.scriptFiles.add(value);
		}
		return this;
	}
	
	/**
	 * 创建脚本的模块
	 * @return {@link Module}
	 */
	public Module<?> getModule() {
		return module;
	}

	/**
	 * 服务资源库
	 * @return {@link ServiceRepository},可能返回null.
	 */
	public ServiceRepository getServiceRepository() {
		return serviceRepository;
	}

	/**
	 * 脚本对象
	 * @return {@link Map}
	 */
	public Map<String, Object> getObjectMap() {
		return this.objectMap == null ? 
				Collections.emptyMap(): 
					Collections.unmodifiableMap(this.objectMap);
	}

	/**
	 * 要加载的脚本文件集合
	 * @return {@link List}
	 */
	public List<ScriptFile> getScriptFiles() {
		return this.scriptFiles == null ?
				Collections.emptyList() :
					Collections.unmodifiableList(this.scriptFiles);
	}

	public final ScriptEngine build() 
			throws ScriptEngineNotFoundException, ScriptException, IOException{
		//检查
		Objects.requireNonNull(this.module, "\"module\" cannot be null.");
		Objects.requireNonNull(serviceRepository, "\"serviceRepository\" cannot be null.");
		Objects.requireNonNull(this.objectMap, "\"objectMap\" cannot be null.");
		
		return this.doBuild();
	}
		
	protected abstract ScriptEngine doBuild() 
			throws ScriptEngineNotFoundException, ScriptException, IOException;
	
}
